package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Collection;
import drh.dao.BaseDao;
import drh.dao.CollectionDao;
import handler.CollectionResultSetHandler;

public class CollectionDaoImpl extends BaseDao implements CollectionDao {

	@Override
	public Integer deleteCollectionById(Integer id) {
	try {
		return query.update("delete from collection where id=?",id);
	} catch (SQLException e) {
		e.printStackTrace();
	}
		return -1;
	}

	@Override
	public Integer updateCollection(Collection info) {
		try {
			return query.update("update collection set productid=?,userid=?,time=?,isdelete=? where id=?",
					info.getProductid(),info.getUserid(),info.getTime(),info.getIsdelete(),info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer addCollection(Collection info) {
	try {
		return query.update("insert into collection(productid,userid,time,isdelete) values(?,?,?,?)",
				info.getProductid(),info.getUserid(),info.getTime(),info.getIsdelete());
	} catch (SQLException e) {
		e.printStackTrace();
	} 
		return -1;
	}

	@Override
	public List<Collection> GetCollectionPaging(Integer pageIndex, Integer pageSize) {
		try {
			return query.query("select * from collection limit ?,?", new CollectionResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Integer getCount() {
		String sql = "select count(1) from collection";
		Object query2;
		try {
			 query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
	
			e.printStackTrace();
		}
		
		return null;
	}

}
